import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../models/contract_model.dart';
import '../../services/contract_service.dart';
import 'reminder_controller.dart';

class ReminderView extends StatelessWidget {
  final ContractService contractService = Get.find();

  @override
  Widget build(BuildContext context) {
    var logic = Get.find<ReminderController>();
    return Scaffold(
      appBar: AppBar(
        title: const Text('到期提醒'),
        actions: [
          IconButton(
            icon: const Icon(Icons.refresh),
            onPressed: () => logic.loadExpiringContracts(),
          )
        ],
      ),
      body: Obx(() {
        final contracts = logic.dataList;
        if (contracts.isEmpty) {
          return const Center(
            child: Text('暂无即将到期的合同'),
          );
        }

        return Padding(
          padding: const EdgeInsets.all(16),
          child: DataTable2(
            columnSpacing: 12,
            horizontalMargin: 12,
            minWidth: 600,
            columns: const [
              DataColumn(label: Text('合同编号')),
              DataColumn(label: Text('客户名称')),
              DataColumn(label: Text('项目名称'), numeric: true),
              DataColumn(label: Text('到期日期')),
              DataColumn(label: Text('剩余天数'), numeric: true),
              DataColumn(label: Text('操作')),
            ],
            rows: contracts.map((contract) {
              final daysLeft =
                  contract.expirationDate.difference(DateTime.now()).inDays;
              return DataRow(
                cells: [
                  DataCell(Text(contract.contractNumber)),
                  DataCell(Text(contract.customerName)),
                  DataCell(Text(contract.projectName)),
                  DataCell(Text(contract.expirationDate
                      .toLocal()
                      .toString()
                      .split(' ')[0])),
                  DataCell(Text('$daysLeft 天')),
                  DataCell(
                    Row(
                      children: [
                        IconButton(
                          icon: const Icon(Icons.autorenew, size: 20),
                          onPressed: () => _handleRenew(contract),
                        ),
                        IconButton(
                          icon: const Icon(Icons.cancel, size: 20),
                          onPressed: () => _handleAbandon(contract),
                        ),
                      ],
                    ),
                  ),
                ],
              );
            }).toList(),
          ),
        );
      }),
    );
  }

  void _handleRenew(Contract contract) {
    Get.defaultDialog(
      title: '续约确认',
      content: Text('确定要续约合同 ${contract.contractNumber} 吗？'),
      actions: [
        TextButton(
          onPressed: () {
            contractService.handleRenew(contract);
            Get.back();
          },
          child: const Text('确定'),
        ),
        TextButton(
          onPressed: () => Get.back(),
          child: const Text('取消'),
        ),
      ],
    );
  }

  void _handleAbandon(Contract contract) {
    Get.defaultDialog(
      title: '放弃确认',
      content: Text('确定要放弃合同 ${contract.contractNumber} 吗？'),
      actions: [
        TextButton(
          onPressed: () {
            contractService.handleAbandon(contract);
            Get.back();
          },
          child: const Text('确定'),
        ),
        TextButton(
          onPressed: () => Get.back(),
          child: const Text('取消'),
        ),
      ],
    );
  }
}
